/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBeans;

import facade.SuiLibFacade;
import facade.exception.IncorrectPasswordException;
import facade.exception.NotExistingObjectException;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import valueObject.UserVO;

/**
 *
 * @author David
 */
@ManagedBean
public class loginFormMB implements Serializable{

    private static final String USER_SESSION_KEY = "user";
    private static final String ROLE_SESSION_KEY = "role";
    private String username;
    private String password;
    private String error;
    private boolean isError=false;
    @EJB
    private SuiLibFacade suiLibFacade;
    /**
     * Creates a new instance of loginFormMB
     */
    public loginFormMB() {
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }
    
    public boolean isLogued() {
        return FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(USER_SESSION_KEY) != null;
    }
    
    public String logout() {
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        return "index";
    }
    
    
    public String login() {
        FacesContext fc = FacesContext.getCurrentInstance();
        String redirect="";
        UserVO userVo = new UserVO();
        userVo.setUsername(getUsername());
        userVo.setPassword(getPassword());
        try {
           UserVO auth = suiLibFacade.logIntoPlatform(userVo);
           switch(auth.getRole().getId()) {
               case 1: redirect="admin";
               break;
               case 2: redirect="university";
               break;
               case 3: redirect="professor";
               break;
               default: throw (new NotExistingObjectException());
           }
            HttpSession session = (HttpSession) fc.getExternalContext().getSession(true);
            session.setAttribute(USER_SESSION_KEY, auth );
            session.setAttribute(ROLE_SESSION_KEY, auth.getRole());
        }
        catch(IncorrectPasswordException ice) {
            this.setError("Contraseña Incorrecta");
            isError=true;
        }
        catch(NotExistingObjectException nee) {
            this.setError("Usuario Inexistente");
            isError=true;
        }
        finally {
            return redirect;
        }
        
    }

    public void setError(String error) {
        this.error = error;
    }
    
    public String getError() {
        return this.error;
    }

    /**
     * @return the isError
     */
    public boolean isIsError() {
        return isError;
    }

    /**
     * @param isError the isError to set
     */
    public void setIsError(boolean isError) {
        this.isError = isError;
    }

}
